package org.pz.camunda.controller;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.pz.camunda.common.StringPoolUtil;
import org.pz.camunda.enums.OperateTypeEnum;
import org.pz.camunda.mapper.VariablesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 请假流程，相关的接口
 * */
@RequestMapping("/askForLevel")
@RestController
@Slf4j
public class AskForLevelController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private VariablesMapper variablesMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 人事节点通过了
     * */
    @RequestMapping("/personnelMattersPass/{taskId}")
    public void personnelMattersPass(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("人事节点审核通过：{}", taskId);

        //下个节点，组长，这里也先写固定
        variables.put("groupLeaderId", "admin");
        //表示同意
        variables.put("personnelMattersFlag", OperateTypeEnum.AGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.AGREE.getCode(), (String)variables.getOrDefault("message", "-"));
    }

    /**
     * 人事节点拒绝
     * */
    @RequestMapping("/personnelMattersReject/{taskId}")
    public void personnelMattersReject(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("人事节点审核驳回：{}", taskId);

        //拒绝标志打上，这个标志配合发起流程节点，判断发起节点是否需要手工操作的
        variables.put(StringPoolUtil.REJECT_FLAG, true);

        //表示拒绝，这个标志配合路由决定流程走向
        variables.put("personnelMattersFlag", OperateTypeEnum.DISAGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.DISAGREE.getCode(), (String)variables.getOrDefault("message", "-"));

    }

    /**
     * 重新发起
     * */
    @RequestMapping("/starterReStart/{taskId}")
    public void starterReStart(@PathVariable String taskId, @RequestBody Map<String,Object> variables) {
        log.info("发起人重新发起：{}", taskId);

        //处理节点
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.RESTART.getCode(),  "-");

    }

    /**
     * 撤销流程
     * */
    @RequestMapping("/starterRevoke/{taskId}")
    public void starterRevoke(@PathVariable String taskId) {
        log.info("发起人撤销流程：{}", taskId);

        //处理节点
        String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId();
        runtimeService.deleteProcessInstance(processInstanceId, "发起人撤销了流程");
    }

    /**
     * 组长节点通过了
     * */
    @RequestMapping("/groupLeaderPass/{taskId}")
    public void groupLeaderPass(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("组长审核通过：{}", taskId);

        //下个节点，组长，这里也先写固定
        List<String> timeList = (List)taskService.getVariable(taskId, "rangePicker");
        if( !Instant.parse( timeList.get(0) ).plusSeconds(60 * 60 * 24).isAfter( Instant.parse( timeList.get(1) ) )){
            //此时说明请假时间超过三天
            variables.put("daysFlag", false);
            //需要设置主管
            variables.put("supervisor", "admin");
        }else{
            variables.put("daysFlag", true);
        }
        //表示同意
        variables.put("groupLeaderFlag", OperateTypeEnum.AGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.AGREE.getCode(), (String)variables.getOrDefault("message", "-"));
    }

    /**
     * 组长节点拒绝
     * */
    @RequestMapping("/groupLeaderReject/{taskId}")
    public void groupLeaderReject(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("组长节点审核驳回：{}", taskId);

        //拒绝标志打上，这个标志配合发起流程节点，判断发起节点是否需要手工操作的
        variables.put(StringPoolUtil.REJECT_FLAG, true);

        //表示拒绝，这个标志配合路由决定流程走向
        variables.put("groupLeaderFlag", OperateTypeEnum.DISAGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.DISAGREE.getCode(), (String)variables.getOrDefault("message", "-"));

    }

    /**
     * 主管节点通过了
     * */
    @RequestMapping("/supervisorPass/{taskId}")
    public void supervisorPass(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("主管节点审核通过：{}", taskId);

        //表示同意
        variables.put("supervisorFlag", OperateTypeEnum.AGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.AGREE.getCode(), (String)variables.getOrDefault("message", "-"));
    }

    /**
     * 主管节点拒绝
     * */
    @RequestMapping("/supervisorReject/{taskId}")
    public void supervisorReject(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> variables) {
        log.info("主管节点审核驳回：{}", taskId);

        //拒绝标志打上，这个标志配合发起流程节点，判断发起节点是否需要手工操作的
        variables.put(StringPoolUtil.REJECT_FLAG, true);

        //表示拒绝，这个标志配合路由决定流程走向
        variables.put("supervisorFlag", OperateTypeEnum.DISAGREE.getCode());
        taskService.complete(taskId, variables);

        //todo 这里应该是当前用户，先写成 admin
        variablesMapper.createProcessTask(taskId, "admin", sdf.format(new Date()), OperateTypeEnum.DISAGREE.getCode(), (String)variables.getOrDefault("message", "-"));

    }

}
